import java.util.concurrent.*;

/**
 * Java 标准库线程池的创建方式总共有几种？分别使用代码创建并演示一下
 *
 */



public class demo2 {
    public static void main1(String[] args) {

    //1.使用 Executors 类中的静态方法创建线程池
        ExecutorService service = Executors.newFixedThreadPool(5);
        service.submit(() ->{
            System.out.println("Hello from thread pool");

        });
        service.shutdown();
    }
//2.使用 ThreadPoolExecutor 类手动创建线程池：
    public static void main2(String[] args) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(2,5,
                10, TimeUnit.SECONDS,new LinkedBlockingQueue<>());
        executor.submit(() ->{
            System.out.println("hello from thread pool");
        });
        executor.shutdown();
    }
//3.使用 ScheduledThreadPoolExecutor 类创建定时任务线程池：
    public static void main(String[] args) {
        ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(3);
        executorService.schedule(() ->{
            System.out.println("hello from thread pool");
        },5,TimeUnit.SECONDS);//数字 5 表示延迟执行任务的时间
        executorService.shutdown();
    }
}

